package com.xfcy.blog.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xfcy.blog.common.constant.RedisConstant;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.entity.Article;
import com.xfcy.blog.entity.Category;
import com.xfcy.blog.entity.Tags;
import com.xfcy.blog.service.ArticleService;
import com.xfcy.blog.service.CategoryService;
import com.xfcy.blog.service.CloudOssService;
import com.xfcy.blog.service.TagsService;
import com.xfcy.blog.service.impl.ArticleServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 晓风残月Lx
 * @date 2023/5/13 16:53
 */
@Component("blogQuartz")
public class BlogQuartz {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ArticleService articleService;
    @Resource
    private CloudOssService cloudOssService;
    @Resource
    private TagsService tagsService;
    @Resource
    private CategoryService categoryService;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     * 自动更新阅读数、点赞数，每天凌晨四点更新
     */
    public void updateReadQuantity() {
        List<Article> blogArticles = new ArrayList<>();
        // 获取带阅读量的前缀key集合
        Map<String, Object> entries = redisTemplate.opsForHash().entries(RedisConstant.ARTICLE_VIEWS_COUNT);
        // 取出数据
        for (Map.Entry<String, Object> stringObjectEntry : entries.entrySet()) {
            String id = stringObjectEntry.getKey();
            Long value = (Long) stringObjectEntry.getValue();
            Article article = new Article();
            article.setId(Long.parseLong(id));
            article.setQuantity(value);
            blogArticles.add(article);
        }
        articleService.updateBatchById(blogArticles);
    }

    /**
     * 删除文件储存中数据库中没有的图片
     */
    public synchronized void removeImg() {
        Set<String> difference = redisTemplate.opsForSet().difference(RedisConstant.FILE_UPLOAD_IMAGES, RedisConstant.DB_FILE_UPLOAD_IMAGES);
        assert difference != null;
        cloudOssService.delBatchFile(difference.toArray(new String[0]));
        redisTemplate.delete(RedisConstant.FILE_UPLOAD_IMAGES);
        redisTemplate.delete(RedisConstant.DB_FILE_UPLOAD_IMAGES);

    }


    /**
     * 删除redis当天验证码通过的ip
     *
     */
    public void removeCode(){

    }

    /**
     * 每天定时修改标签和分类的点击量
     */
    public void updateTagsAndCategoryClickVolume() {
        Map<String, Object> tagMap = redisTemplate.opsForHash().entries(RedisConstant.TAG_CLICK_VOLUME);
        List<Tags> tagsList = new ArrayList<>();
        for (Map.Entry<String, Object> stringObjectEntry : tagMap.entrySet()) {
            String id = stringObjectEntry.getKey();
            Integer value = (Integer) stringObjectEntry.getValue();
            Tags tags = new Tags(Long.parseLong(id), value);
            tagsList.add(tags);
        }

        Map<String, Object> categoryMap = redisTemplate.opsForHash().entries(RedisConstant.TAG_CLICK_VOLUME);
        List<Category> categoryList = new ArrayList<>();
        for (Map.Entry<String, Object> stringObjectEntry : categoryMap.entrySet()) {
            String id = stringObjectEntry.getKey();
            Integer value = (Integer) stringObjectEntry.getValue();
            Category category = new Category(Long.parseLong(id), value);
            categoryList.add(category);
        }
        tagsService.updateBatchById(tagsList);
        categoryService.updateBatchById(categoryList);
    }


}
